<?php
class Password extends Controller {
	public function html() {
		$mUser = new User_model();
		$currUser = $mUser->currUser();
		if(!$currUser) header('Location:/auth/login?backurl='.urlencode('/profile/password'));
		
		$data = array();
		$tpl = 'profile/password.html';
		
		$this->display($data, $tpl);
	}
	public function json() {
		$mUser = new User_model();
		$currUser = $mUser->currUser();
		
		$password = $this->post('password', 'str');
		$password1 = $this->post('password1', 'str');
		$password2 = $this->post('password2', 'str');
		
		if(!$password) $this->displayJSON(array('status' => -1, 'message' => '请输入原密码'));
		if(!$password1) $this->displayJSON(array('status' => -2, 'message' => '请输入新密码'));
		if(!$password2) $this->displayJSON(array('status' => -3, 'message' => '请重复输入密码'));
		if($password1 != $password2) $this->displayJSON(array('status' => -4, 'message' => '两次输入密码不一致'));
		
		$mUser = new User_model();
		
		$user = $mUser->userInfoByUid($currUser['uid']);
		if(!$user) $this->displayJSON(array('status' => -5, 'message' => '未知用户'));
		
		if(md5($password) != $user['password']) $this->displayJSON(array('status' => -6, 'message' => '原密码错误'));
		
		$cols = array('password' => $password);
		$result = $mUser->editUser($currUser['uid'], $cols);
		if($result) $this->displayJSON(array('status' => 0, 'message' => '修改密码成功'));
		else $this->displayJSON(array('status' => -7, 'message' => '修改密码失败'));
	}
}